import torch
import numpy as np
import matplotlib.pyplot as plt
11wk-1: 순환신경망 (4)
순환신경망
RNN (2)– AbAcAd예제(3)
강의영상
https://youtube.com/playlist?list=PLQqh36zP38-zRWSBjuvzsjPe6JxnHO4AX
import
Define some funtions
def f(txt,mapping):
return [mapping[key] for key in txt]
= torch.nn.Softmax(dim=1) soft
Exam4: AbAcAd (3)
data
-
기존의 정리방식
= list('AbAcAd')*100
txt 10] txt[:
['A', 'b', 'A', 'c', 'A', 'd', 'A', 'b', 'A', 'c']
= txt[:-1]
txt_x = txt[1:] txt_y
5],txt_y[:5] txt_x[:
(['A', 'b', 'A', 'c', 'A'], ['b', 'A', 'c', 'A', 'd'])
= torch.nn.functional.one_hot(torch.tensor(f(txt_x,{'A':0,'b':1,'c':2,'d':3}))).float()
x = torch.nn.functional.one_hot(torch.tensor(f(txt_y,{'A':0,'b':1,'c':2,'d':3}))).float() y
x,y
(tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[1., 0., 0., 0.],
...,
[1., 0., 0., 0.],
[0., 0., 1., 0.],
[1., 0., 0., 0.]]),
tensor([[0., 1., 0., 0.],
[1., 0., 0., 0.],
[0., 0., 1., 0.],
...,
[0., 0., 1., 0.],
[1., 0., 0., 0.],
[0., 0., 0., 1.]]))
HW: hello 예제
아래와 같이 hello가 반복되는 자료가 있다고 하자.
= list('hello')*100
txt 10] txt[:
['h', 'e', 'l', 'l', 'o', 'h', 'e', 'l', 'l', 'o']
= txt[:-1]
txt_x = txt[1:] txt_y
5],txt_y[:5] txt_x[:
(['h', 'e', 'l', 'l', 'o'], ['e', 'l', 'l', 'o', 'h'])
= torch.nn.functional.one_hot(torch.tensor(f(txt_x,{'h':0,'e':1,'l':2,'o':3}))).float()
x = torch.nn.functional.one_hot(torch.tensor(f(txt_y,{'h':0,'e':1,'l':2,'o':3}))).float() y
x,y
(tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
...,
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 1., 0.]]),
tensor([[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 1., 0.],
...,
[0., 0., 1., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]]))
3개의 은닉노드를 가진 RNN을 설계하고 학습시켜라.